package com.koushikdutta.ion;

import android.annotation.TargetApi;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory.Options;
import android.graphics.BitmapRegionDecoder;
import android.graphics.Point;
import android.graphics.Rect;
import android.text.TextUtils;
import com.koushikdutta.async.future.FutureCallback;
import com.koushikdutta.async.util.FileCache;
import com.koushikdutta.async.util.StreamUtility;
import com.koushikdutta.ion.bitmap.BitmapInfo;
import com.koushikdutta.ion.gif.GifAction;
import com.koushikdutta.ion.gif.GifDecoder;
import java.io.File;
import java.io.FileInputStream;

@TargetApi(10)
public class LoadDeepZoom extends LoadBitmapEmitter implements FutureCallback<File> {
    FileCache fileCache;

    public LoadDeepZoom(Ion ion, String urlKey, boolean animateGif, EmitterTransform<File> emitterTransform, FileCache fileCache) {
        super(ion, urlKey, true, animateGif, emitterTransform);
        this.fileCache = fileCache;
    }

    public void onCompleted(Exception e, final File tempFile) {
        if (e != null) {
            report(e, null);
        } else if (this.ion.bitmapsPending.tag(this.key) == this) {
            Ion.getBitmapLoadExecutorService().execute(new Runnable() {
                public void run() {
                    FileInputStream fin = null;
                    try {
                        File file;
                        if (LoadDeepZoom.this.fileCache != null) {
                            LoadDeepZoom.this.fileCache.commitTempFiles(LoadDeepZoom.this.key, tempFile);
                            file = LoadDeepZoom.this.fileCache.getFile(LoadDeepZoom.this.key);
                        } else {
                            file = tempFile;
                        }
                        Options options = LoadDeepZoom.this.ion.getBitmapCache().prepareBitmapOptions(file, 0, 0);
                        Point size = new Point(options.outWidth, options.outHeight);
                        BitmapInfo info;
                        if (LoadDeepZoom.this.animateGif && TextUtils.equals("image/gif", options.outMimeType)) {
                            fin = LoadDeepZoom.this.fileCache.get(LoadDeepZoom.this.key);
                            GifDecoder decoder = new GifDecoder(fin, new GifAction() {
                                public boolean parseOk(boolean parseStatus, int frameIndex) {
                                    return LoadDeepZoom.this.animateGif;
                                }
                            });
                            decoder.run();
                            if (decoder.getFrameCount() == 0) {
                                throw new Exception("failed to load gif");
                            }
                            Bitmap[] bitmaps = new Bitmap[decoder.getFrameCount()];
                            int[] delays = decoder.getDelays();
                            for (int i = 0; i < decoder.getFrameCount(); i++) {
                                Bitmap bitmap = decoder.getFrameImage(i);
                                if (bitmap == null) {
                                    throw new Exception("failed to load gif frame");
                                }
                                bitmaps[i] = bitmap;
                            }
                            info = new BitmapInfo(LoadDeepZoom.this.key, options.outMimeType, bitmaps, size);
                            info.delays = delays;
                            if (LoadDeepZoom.this.emitterTransform != null) {
                                info.loadedFrom = LoadDeepZoom.this.emitterTransform.loadedFrom();
                            } else {
                                info.loadedFrom = 1;
                            }
                            LoadDeepZoom.this.report(null, info);
                            StreamUtility.closeQuietly(fin);
                            return;
                        }
                        BitmapRegionDecoder decoder2 = BitmapRegionDecoder.newInstance(file.toString(), false);
                        if (decoder2.decodeRegion(new Rect(0, 0, size.x, size.y), options) == null) {
                            throw new Exception("unable to load decoder");
                        }
                        info = new BitmapInfo(LoadDeepZoom.this.key, options.outMimeType, new Bitmap[]{decoder2.decodeRegion(new Rect(0, 0, size.x, size.y), options)}, size);
                        info.decoder = decoder2;
                        info.decoderFile = file;
                        info.loadedFrom = 3;
                        LoadDeepZoom.this.report(null, info);
                        StreamUtility.closeQuietly(null);
                    } catch (Exception e) {
                        LoadDeepZoom.this.report(e, null);
                        StreamUtility.closeQuietly(fin);
                    } catch (Throwable th) {
                        StreamUtility.closeQuietly(fin);
                    }
                }
            });
        }
    }
}
